package com.inspur.edp.common.expr.expresses.logic;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.inspur.edp.common.expr.ExprListJsonDeserializer;
import com.inspur.edp.common.expr.Express;
import io.iec.edp.caf.commons.utils.StringUtils;
import lombok.Data;

import java.io.IOException;
import java.util.List;

/**
 * @author lizhaorui
 * @description TODO
 * @date 2024/12/3 9:13
 */

@Data
public class LogicExpr implements Express {

    public static final String KIND = "logic";

    @JsonDeserialize(using = LogicOperatorDeserializer.class)
    private LogicOperator operator;

    @JsonDeserialize(using = ExprListJsonDeserializer.class)
    private List<Express> expresses;

    @Override
    public String getKind() {
        return KIND;
    }

    public static class LogicOperatorDeserializer extends JsonDeserializer<LogicOperator> {

        @Override
        public LogicOperator deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
            JsonNode node = jsonParser.getCodec().readTree(jsonParser);
            if (node == null || StringUtils.isEmpty(node.asText())) {
                return null;
            }
            return LogicOperator.valueOf(node.asText().toUpperCase());
        }
    }

}
